package com.cc.Dao;

import org.apache.log4j.Logger;

import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

/**
 * @author CC
 * @version 1.0
 * @description: TODO
 * @date 2025/9/26 9:33
 */
public class BaseDao {
    private static final Logger log = Logger.getLogger(BaseDao.class);
    //驱动
    //驱动
    private static String DRIVER = null;
    //数据库连接地址
    private static String URL = null;
    //用户名
    private static String USER = null;
    //密码
    private static String PWD = null;
    //数据库连接对象
    Connection conn = null;
    //指令操作对象
    PreparedStatement pstmt = null;
    //结果集
    ResultSet rs = null;

    //静态代码块
    static {
        //通过类加载器获取输入流
        String configFile = "database.properties";
        InputStream is = BaseDao.class.getClassLoader().getResourceAsStream(configFile);
        Properties properties = new Properties();
        try {
            properties.load(is);
            DRIVER = properties.getProperty("DRIVER");
            URL = properties.getProperty("URL");
            USER = properties.getProperty("USER");
            PWD = properties.getProperty("PWD");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        try {
            //加载驱动
            Class.forName(DRIVER);
        } catch (ClassNotFoundException e) {
            log.error("加载驱动失败！");
            e.printStackTrace();
        }
    }

    //创建数据库连接对象
    public Connection getConnection() {
        try {
            conn = DriverManager.getConnection(URL, USER, PWD);
            log.debug("获取数据库连接成功！");
        } catch (SQLException e) {
            log.error("获取数据库连接失败！");
            e.printStackTrace();
        }
        return conn;
    }

    //执行增删改方法
    public int executeUpdate(String sql, Object[] params) {
        //打开数据库连接
        conn = getConnection();
        try {
            //创建指令操作对象
            pstmt = conn.prepareStatement(sql);
            //设置参数
            if (params != null && params.length > 0) {
                for (int i = 0; i < params.length; i++) {
                    pstmt.setObject(i + 1, params[i]);
                }
            }
            // 执行操作
            log.debug("执行增删改操作开始,sql:"+ sql);
            log.debug("执行操作成功！");
            return pstmt.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            //关闭数据库连接
            close(null);
        }
    }

    //执行查询方法
    public ResultSet executeQuery(String sql, Object[] params) {
        //打开数据库连接
        conn = getConnection();
        //创建指令操作对象
        try {
            pstmt = conn.prepareStatement(sql);
            //设置参数
            if (params != null && params.length > 0) {
                for (int i = 0; i < params.length; i++) {
                    pstmt.setObject(i + 1, params[i]);
                }
            }
            log.debug("执行查询操作,sql:"+ sql);
            return pstmt.executeQuery();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
    //关闭数据库连接
    public void close(ResultSet rs) {
        try {
            if (rs != null) {
                rs.close();
            }
            if (pstmt != null) {
                pstmt.close();
            }
            if (conn != null) {
                conn.close();
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}
